<template>
  <div>
    <div>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['advertising:location:locationRules:add']"
            style="margin: 20px 20px 20px 20px"
          >
            新增广告位置配置
          </el-button>
        </el-col>
      </el-row>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'postId', order: 'descending' }"
    >
      <el-table-column align="center" label="名称" prop="title" />
      <el-table-column align="center" label="是否已启用" prop="isEnable">
        <template #default="scope">
          <span v-if="scope.row.isEnable == 0">否</span>
          <span v-if="scope.row.isEnable == 1">是</span>
          <span v-if="scope.row.isEnable != 0 && scope.row.isEnable != 1"
            >未知</span
          >
        </template>
      </el-table-column>
      <el-table-column align="center" label="平台" prop="isEnable">
        <template #default="scope">
          <span v-if="scope.row.sourcePlatform == 0">APP</span>
          <span v-if="scope.row.sourcePlatform == 1">PC</span>
          <span
            v-if="
              scope.row.sourcePlatform != 0 && scope.row.sourcePlatform != 1
            "
            >未知</span
          >
        </template>
      </el-table-column>
      <el-table-column align="center" label="可投放数量" prop="number" />
      <el-table-column
        align="center"
        label="剩余可投放数量"
        prop="surplusNumber"
      />
      <el-table-column align="center" label="添加时间" prop="createTime" />
      <el-table-column align="center" label="备注" prop="remarks" />
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['advertising:location:locationRules:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['advertising:location:locationRules:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="是否启用" prop="isEnable">
          <el-radio-group v-model="form.isEnable">
            <el-radio :value="0">否</el-radio>
            <el-radio :value="1">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="平台" prop="sourcePlatform">
          <el-radio-group v-model="form.sourcePlatform">
            <el-radio :value="0">APP</el-radio>
            <el-radio :value="1">PC</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="可投放数量" prop="number">
          <el-input
            type="number"
            v-model="form.number"
            :min="0"
            style="width: 100px"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import {
  getPositionApi,
  addPositionApi,
  deletePositionApi,
  getPositionByIdApi,
  putPositionApi,
} from "@/api/advertising/location";
const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");
const data = reactive({
  form: {
    title: "",
    isEnable: 0, //是否启用0：否 1：是
    sourcePlatform: 0, //0:APP 1:PC
    number: 0, //可投放数量
    remarks: "", //备注
  },
  rules: {
    title: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  },
});
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  title: "",
});

const { form, rules } = toRefs(data);

/** 查询广告位置配置列表 */
function getList() {
  loading.value = true;
  getPositionApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增广告位置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.id;
  getPositionByIdApi(postId).then((response) => {
    form.value = {
      id: response.data.id,
      title: response.data.title,
      isEnable: response.data.isEnable,
      sourcePlatform: Number(response.data.sourcePlatform),
      number: response.data.number,
      remarks: response.data.remarks,
    };
    form.value.sourcePlatform = Number(form.value.sourcePlatform);
    open.value = true;
    title.value = "修改广告位置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        putPositionApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPositionApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    title: "",
    isEnable: 0,
    sourcePlatform: 0,
    number: 0,
    remarks: "",
  };
  proxy.resetForm("postRef");
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id;
  proxy.$modal
    .confirm('是否确认删除广告位置配置为"' + row.title + '"的数据项？')
    .then(function () {
      return deletePositionApi(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style scoped>
:deep(.el-form-item) {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
